package com.example.service;

import com.example.dao.NoticeDao;
import com.example.entity.Notice;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NoticeService {

    @Resource
    private NoticeDao noticeDao;

    public List<Notice> findAll() {
        return noticeDao.selectAll();
    }

    public PageInfo<Notice> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Notice> list = noticeDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Notice notice) {
        if(notice.getTitle() == null || notice.getTitle().trim().equals("")) {
            throw new CustomException("公告标题不能为空");
        }
        noticeDao.insertSelective(notice);
    }

    public void update(Notice notice) {
        noticeDao.updateByPrimaryKeySelective(notice);
    }

    public void delete(Integer noticeId) {
        noticeDao.deleteByPrimaryKey(noticeId);
    }

    public Notice findById(Integer noticeId) {
        return noticeDao.selectByPrimaryKey(noticeId);
    }
}
